﻿using System;
using System.Collections.Generic;
using Authority.Logic;
using Authority.Model;
using Common.Library;
using Container.Library;
using Microsoft.AspNetCore.Mvc;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Authority.Controllers
{
    /// <summary>
    /// 角色管理控制器
    /// </summary>
    [Area("Authority")]
    public class RoleController : BaseController
    {
        private readonly ISysRoleService roleService = null;//角色管理逻辑
        private readonly ISysMenuService menuService = null;// 角色管理逻辑

        public RoleController()
        {
            //角色管理逻辑注入
            roleService = UnityCIContainer.Instance.GetService<ISysRoleService>();
            //角色管理逻辑注入
            menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();
        }


        #region 界面
        /// <summary>
        /// 角色列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 增加界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 修改界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Modify()
        {
            return View();
        }

        /// <summary>
        /// 授权界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Authorize()
        {
            return View();
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 获取所有的可操作角色 
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadListInfo()
        {
            var resultInfo = new ResultJsonInfo<List<SysRoleEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                resultInfo = roleService.LoadListInfo(parametersInfo.parameters,RoleType.Company);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取所有的角色列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadRoleSelectList()
        {
            var resultInfo = new ResultJsonInfo<List<SelectListInfo>>();
            try
            {
                resultInfo = roleService.LoadSysRoleAllList();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 新增根节点
        /// <summary>
        /// 新增根节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.AddRoot)]
        public ActionResult AddRoot()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<RoleRootRequest>>();

                resultInfo = roleService.AddRoot(parametersInfo.parameters, RoleType.Company);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 新增子节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Add)]
        public ActionResult AddChild()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<RoleChildRequest>>();

                resultInfo = roleService.AddChild(parametersInfo.parameters, RoleType.Company);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 修改节点信息
        /// <summary>
        /// 修改节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Edit)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<RoleModifyRequest>>();

                resultInfo = roleService.Modify(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Remove)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<string>>>();

                resultInfo = roleService.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 权限操作

        /// <summary>
        /// 获取所有的可操作菜单
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadMenusListInfo()
        {
            var resultInfo = new ResultJsonInfo<List<RoleMenusListResponse>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                resultInfo = menuService.LoadMenusListInfoByRoleCode(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 根据角色code和菜单code获取可操作按钮
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadMenuButtonListByRoleMenuCode()
        {
            var resultInfo = new ResultJsonInfo<List<RoleMenuButtonsListResponse>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<RoleMenuButtonQueryRequest>>();

                resultInfo = roleService.LoadMenuButtonListByRoleMenuCode(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 更改角色-菜单按钮关系表信息（给角色授按钮权限）
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ModifyRoleMenuButtonInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<RoleMenuButtonModifyRequest>>();

                resultInfo = roleService.ModifyRoleMenuButtonInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 给角色授菜单权限
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ModifyRoleMenuInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<RoleMenuModifyRequest>>();

                resultInfo = roleService.ModifyRoleMenuInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}